﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace ZLDC.Common.Estate
{
    public class _Tools
    {
        public static string GetIp()
        {
            return System.Web.HttpContext.Current.Request.UserHostAddress;
        }

        public static string GetUrl()
        {
            return HttpContext.Current.Request.Url.ToString();


        }

        public static bool UrlExistsUsingHttpWebRequest(string url)
        {
            try
            {
                System.Net.HttpWebRequest myRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
                myRequest.Method = "HEAD";
                myRequest.Timeout = 10000;
                System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)myRequest.GetResponse();
                return (res.StatusCode == System.Net.HttpStatusCode.OK);
            }
            catch (System.Net.WebException we)
            {
                System.Diagnostics.Trace.Write(we.Message);
                return false;
            }
        }

        public static string TrimBlank(string str, char TrimChar)
        {
            string result = "";

            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] != TrimChar)
                {
                    result += str[i];
                }
            }

            return result;
        }

        public static string TrimBlank(string str)
        {
            return TrimBlank(str, ' ');
        }

        public static string CompressDigit(string sIn)
        {
            string[] sAlphaList = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s",
					"t","u","v","w","x","y","z","A","B","C","D","E","F","G","H"};
            int i = 0;
            string sRtn = sIn.Replace("400", sAlphaList[i++]);
            for (int j = 0; j < 3; j++)
            {
                sRtn = sRtn.Replace("a" + j.ToString(), sAlphaList[i++]);
            }

            for (int j = 0; j < 10; j++)
            {
                sRtn = sRtn.Replace("b" + j.ToString(), sAlphaList[i++]);
            }

            for (int j = 0; j < 10; j++)
            {
                sRtn = sRtn.Replace("c" + j.ToString(), sAlphaList[i++]);
            }

            for (int j = 0; j < 10; j++)
            {
                sRtn = sRtn.Replace("d" + j.ToString(), sAlphaList[i++]);
            }

            return sRtn;
        }

        public static string UncompressDigit(string sIn)
        {
            string[] sAlphaList = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s",
									  "t","u","v","w","x","y","z","A","B","C","D","E","F","G","H"};
            string sRtn = sIn;
            int i = 33;
            for (int j = 9; j > -1; j--)
            {
                sRtn = sRtn.Replace(sAlphaList[i--], "d" + j.ToString());
            }

            for (int j = 9; j > -1; j--)
            {
                sRtn = sRtn.Replace(sAlphaList[i--], "c" + j.ToString());
            }

            for (int j = 9; j > -1; j--)
            {
                sRtn = sRtn.Replace(sAlphaList[i--], "b" + j.ToString());
            }

            for (int j = 2; j > -1; j--)
            {
                sRtn = sRtn.Replace(sAlphaList[i--], "a" + j.ToString());
            }

            sRtn = sRtn.Replace(sAlphaList[i--], "400");
            return sRtn;
        }

        public static bool IsExistsStr(string str, string childstr)
        {
            bool flag = false;

            try
            {
                string[] arr = str.Split(',');
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i].Equals(childstr))
                    {
                        flag = true;
                        break;
                    }
                }
            }
            catch { }

            return flag;
        }

    }
}
